package com.yDev.rockclimber;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Random;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.os.Environment;

public class Wall {

	String mScreenshotPath = Environment.getExternalStorageDirectory()+"";
	
	
	String mapName = "route1.txt";
	
	Paint paint;
	ArrayList<Rock> rocks;
	Random rand;
	Bitmap all;
	public Wall(Context context) {
		paint = new Paint();
		rocks = new ArrayList<Rock>();
		rand = new Random();
		
		all=Bitmap.createScaledBitmap(
				BitmapFactory.decodeResource(context.getResources(), R.drawable.rock1),
				50, 50, true);
	}
	
	public void addRock(Rock r){
		rocks.add(r);
	}

	public void generateWall(Point limit) {

		Rock r;

		for (int i = 0; i < 100; i++) {
			r = new Rock();
			r.size = rand.nextInt(15);
			r.location = new Point(rand.nextInt(limit.x), rand.nextInt(limit.y));
			rocks.add(r);
		}

	}

	public void draw(Canvas canvas, Context context, int offset) {
		if (rocks.size() > 0) {
			for (Rock r : rocks) {
				paint.setColor(Color.BLACK);
				canvas.drawBitmap(all, r.location.x, r.location.y+offset, paint);
			}
		}
	}

	public void saveMap(String string) {
		String content;
		for (Rock r : rocks) {
			content=r.size + " " + r.rockImageID + " " + r.location.x + " " + r.location.y + "\n";
			try {
				MyFiles.createFileOrAppend(mScreenshotPath + "/" + mapName, content);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}
	
	public void loadMap(String string){
		int line=0;
		Rock r;
		Point p;
		while(MyFiles.readFileLine(mScreenshotPath + "/" + mapName, line) != null){
			String l = MyFiles.readFileLine(mScreenshotPath + "/" + mapName, line);
			if(l.compareTo("")!=0){
			String[] tokens = l.split(" ");
			p=new Point(Integer.parseInt(tokens[2]),Integer.parseInt(tokens[3]));
			
			r=new Rock(Integer.parseInt(tokens[0]), Integer.parseInt(tokens[1]), p);
			addRock(r);
			}
			line++;
			
		}
	}

}
